<?php
session_start();
include_once("conn.php");

function Check_Password($password1,$password2)
{
	$Max_Strlen_Password=16;
	$Min_Strlen_Password=6;
	$PasswordChars="^[a-zA-Z0-9_-]";
	$PasswordGood="用户密码正确";
	if($password1!=$password2)
	{
		$PasswordGood="前后密码不一致";
		return $PasswordGood;
	}
	if(strlen($password1)>$Max_Strlen_Password || strlen($password1)<$Min_Strlen_Password)
	{
		$PasswordGood="密码长度错误";
		return $PasswordGood;
	}
	if(!ereg($PasswordChars,$password1))
	{
		$PasswordGood="用户密码包含非法字符";
		return $PasswordGood;
	}
	return $PasswordGood;


}
function Check_Email($email)
{
	
	$EmailChars="^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$";//正则表达式
	$EmailGood="邮箱正确";
	if($email=="")
	{
		$EmailGood="邮箱不能为空";
		return $EmailGood;
	}
	if(!ereg($EmailChars,$email))
	{
		$EmailGood="邮箱地址包含非法字符";
		return $EmailGood;
	}
	$query="select * from tb_user_info where email='".$email."'";
	$result=mysql_query($query);
	$SameName=mysql_fetch_array($result);
	if($SameName["name"]!=NULL) //只要能查出一条来
	{
		$EmailGood="邮箱已经被注册";
	}
	return $EmailGood;

}



$oldemail = $_POST["oldemail"];
$newpass = $_POST["newpass"];
$newpass2 = $_POST["newpass2"];
$oldpass = $_POST["oldpass"];

$query = "select * from tb_user_info where password='".$oldpass."' && name='".$_SESSION["current_user"]."'";
$result = mysql_query($query);
if( mysql_num_rows($result)!=NULL )
{
	$PasswordGood=Check_Password($newpass,$newpass2);
	$EmailGood=Check_Email($oldmail);
	if($PasswordGood=="用户密码正确")
	{
		$query = "update tb_user_info set password='".$newpass."' where name='".$_SESSION["current_user"]."'";
		$result = mysql_query($query);
		$PasswordGood=" 密码更新成功 ";
		echo $PasswordGood;
	}
	else
	{
		echo $PasswordGood;
	}
	if($EmailGood=="邮箱正确")
	{
		$query = "select * from tb_user_info where name='".$_SESSION["current_user"]."' && email='".$oldemail."' ";
		mysql_query($query);
		if(mysql_affected_rows()==NULL)
		{
			$query = "update tb_user_info set email='".$oldemail."' where name='".$_SESSION["current_user"]."'";
			mysql_query($query);
			$EamilGood=" 邮箱更新成功 ";
			echo $EmailGood;
		}

	}
	else
	{
		echo $EmailGood;
	}
}

$arr['p'] = $PasswordGood;
$arr['e'] = $EmailGood;

echo json_encode($arr);

?>